	package edu.gatech.cs2340.bytebankers.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.sound.midi.VoiceStatus;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import edu.gatech.cs2340.bytebankers.presenter.IStartupPresenter;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class UIStartUpView extends JFrame implements IStartupView {
    private JPanel contentPane;
    private JTextField txtLeader;
    private JTextField txtPlayer1;
    private JTextField txtPlayer2;
    private JTextField txtPlayer3;
    private IStartupPresenter presenter;
    private JPanel centerPanel;
    private Map<JRadioButton, String> professionButtonMap;
    private ButtonGroup professionButtonGroup;
    private JTextField txtPlayer4;
    JButton btnLoadGame;
    JComboBox cmbPace;
    JComboBox cmbRation;

    public UIStartUpView(IStartupPresenter presenter) {
	this();
	this.presenter = presenter;
	professionButtonMap = new HashMap<JRadioButton, String>();
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
	    public void run() {
		try {
		    UIStartUpView frame = new UIStartUpView();
		    frame.setVisible(true);
		} catch (Exception e) {
		    e.printStackTrace();
		}
	    }
	});
    }

    /**
     * Create the frame.
     */
    public UIStartUpView() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 860, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	
		centerPanel = new JPanel();
		centerPanel.setBounds(300, 0, 660, 500);
		contentPane.add(centerPanel);
		centerPanel.setLayout(null);
	
		txtLeader = new JTextField();
		txtLeader.setBounds(340, 136, 200, 20);
		centerPanel.add(txtLeader);
		txtLeader.setColumns(10);
	
		JLabel lblEnterLeaderName = new JLabel("LEADER'S NAME:\r\n");
		lblEnterLeaderName.setForeground(Color.WHITE);
		lblEnterLeaderName.setBounds(390, 111, 133, 14);
		centerPanel.add(lblEnterLeaderName);
	
		JLabel lblLeadersProfession = new JLabel("LEADER'S PROFESSION");
		lblLeadersProfession.setForeground(Color.WHITE);
		lblLeadersProfession.setBounds(371, 171, 145, 14);
		centerPanel.add(lblLeadersProfession);
	
		JLabel lblNameOtherPlayers = new JLabel("NAME OTHER PLAYERS");
		lblNameOtherPlayers.setForeground(Color.WHITE);
		lblNameOtherPlayers.setBounds(283, 236, 151, 14);
		centerPanel.add(lblNameOtherPlayers);
	
		txtPlayer1 = new JTextField();
		txtPlayer1.setBounds(309, 261, 86, 20);
		centerPanel.add(txtPlayer1);
		txtPlayer1.setColumns(10);
	
		txtPlayer2 = new JTextField();
		txtPlayer2.setBounds(308, 292, 86, 20);
		centerPanel.add(txtPlayer2);
		txtPlayer2.setColumns(10);
	
		txtPlayer3 = new JTextField();
		txtPlayer3.setBounds(309, 323, 86, 20);
		centerPanel.add(txtPlayer3);
		txtPlayer3.setColumns(10);
	
		JButton btnStartGame = new JButton("Start Game\r\n");
		btnStartGame.addMouseListener(new MouseAdapter() {
		    @Override
		    public void mouseClicked(MouseEvent arg0) {
			if (txtLeader.getText().equals("")) {
			    JOptionPane.showMessageDialog(centerPanel,
				    "You must name your leader.");
			    return;
			}
	
			if (txtPlayer1.getText().equals("")
				|| txtPlayer2.getText().equals("")
				|| txtPlayer3.getText().equals("")
				|| txtPlayer4.getText().equals("")) {
			    JOptionPane.showMessageDialog(centerPanel,
				    "You must supply a name for all of the players.");
			    return;
			}
	
			boolean professionIsSelected = false;
	
			for (Map.Entry<JRadioButton, String> e : professionButtonMap
				.entrySet()) {
			    if (e.getKey().isSelected()) {
				professionIsSelected = true;
			    }
			}
	
			if (!professionIsSelected) {
			    JOptionPane.showMessageDialog(centerPanel,
				    "You must select a profession.");
			    return;
			}
	
			onSaveForm();
		    }
		});
		btnStartGame.setBounds(331, 401, 104, 23);
		centerPanel.add(btnStartGame);
	
		txtPlayer4 = new JTextField();
		txtPlayer4.setColumns(10);
		txtPlayer4.setBounds(308, 355, 86, 20);
		centerPanel.add(txtPlayer4);
	
		JLabel lblPace = new JLabel("RATION RATE");
		lblPace.setHorizontalAlignment(SwingConstants.LEFT);
		lblPace.setForeground(Color.WHITE);
		lblPace.setBounds(453, 298, 151, 14);
		centerPanel.add(lblPace);
	
		JLabel label = new JLabel("PACE");
		label.setHorizontalAlignment(SwingConstants.LEFT);
		label.setForeground(Color.WHITE);
		label.setBounds(453, 236, 151, 14);
		centerPanel.add(label);
	
		JLabel lblMealsday = new JLabel("Meals/Day");
		lblMealsday.setHorizontalAlignment(SwingConstants.LEFT);
		lblMealsday.setForeground(Color.WHITE);
		lblMealsday.setBounds(527, 324, 86, 14);
		centerPanel.add(lblMealsday);
	
		cmbPace = new JComboBox();
		cmbPace.setModel(new DefaultComboBoxModel(new String[] { 
				"Very Slow", "Slow", "Medium", "Fast", "Very Fast" }));
		cmbPace.setBounds(447, 258, 116, 27);
		centerPanel.add(cmbPace);
	
		cmbRation = new JComboBox();
		cmbRation.setModel(new DefaultComboBoxModel(new String[] { "1", "2",
			"3", "4" }));
		cmbRation.setBounds(447, 318, 76, 27);
		centerPanel.add(cmbRation);
		
		btnLoadGame = new JButton("Load Game\r\n");
		btnLoadGame.setBounds(435, 401, 104, 23);
		centerPanel.add(btnLoadGame);
	
		btnLoadGame.addMouseListener(new MouseAdapter() {
		    @Override
		    public void mouseClicked(MouseEvent arg0) {
				onLoadGameClick();
		    }
		});
    }

    @Override
    public void onSaveForm() {
	presenter.onSaveForm();
    }

    public JPanel getMainPanel() {
	return centerPanel;
    }

    /**
     * Takes a map of profession and dynamically builds the radio button list.
     */
    public void setProfessions(Map<String, String> professions) {

	professionButtonGroup = new ButtonGroup();

	// Create a panel with flow layout to hold our buttons
	FlowLayout fl = new FlowLayout();
	JPanel jp = new JPanel();
	jp.setLayout(fl);
	jp.setBackground(null);

	// Loop through the professions, creating and adding the radio buttons.
	for (Map.Entry<String, String> e : professions.entrySet()) {
	    JRadioButton rdbtnProfession = new JRadioButton(e.getKey());
	    jp.add(rdbtnProfession);
	    rdbtnProfession.setForeground(new Color(255, 255, 255));
	    professionButtonMap.put(rdbtnProfession, e.getValue());
	    professionButtonGroup.add(rdbtnProfession);
	}

	// finalize and add the panel to the main panel.
	jp.setOpaque(false);
	jp.setBounds(0, 190, 860, 30);
	centerPanel.add(jp);

	// add the background image
	JLabel label = new JLabel("");
	label.setIcon(new ImageIcon(
		GameStartView.class
			.getResource("/edu/gatech/cs2340/bytebankers/graphics/StartupBG.jpg")));

	label.setIcon(new ImageIcon(
		StoreBGView.class
			.getResource("/edu/gatech/cs2340/bytebankers/graphics/StartupBG.jpg")));

	label.setBounds(0, 0, 860, 500);
	centerPanel.add(label);
    }

    /***
     * Returns the name of the leader entered by the user.
     * 
     * @return The leader's name.
     */
    public String getLeaderName() {
	return txtLeader.getText();
    }

    /***
     * Returns the class of the leader selected by the user.
     * 
     * @return The full class of the profesion with namespace.
     */
    public String getProfession() {
	for (Map.Entry<JRadioButton, String> e : professionButtonMap.entrySet()) {
	    if (e.getKey().isSelected()) {
		return e.getValue();
	    }
	}
	return "";
    }

    /***
     * Returns the name of the leader entered by the user.
     * 
     * @return An array of player names chosen by the user.
     */
    public java.util.List<String> getPlayerNames() {
	ArrayList<String> players = new ArrayList<String>();
	players.add(txtPlayer1.getText());
	players.add(txtPlayer2.getText());
	players.add(txtPlayer3.getText());
	players.add(txtPlayer4.getText());
	return players;
    }

    /***
     * Returns the ration rate chosen by the user.
     * 
     * @return The ration rate.
     */
    public int getRationRate() {
	return Integer.parseInt(cmbRation.getSelectedItem().toString());
    }

    /***
     * Returns the pace chosen by the user.
     * 
     * @return The pace.
     */
    @Override
    public int getPace() {
    	int pace = 2;
		switch(cmbPace.getSelectedIndex()){
			case 0:
				pace = 2;
				break;
			case 1:
				pace = 4;
				break;
			case 2:
				pace = 6;
				break;
			case 3:
				pace = 8;
				break;
			case 4:
				pace = 10;
				break;
			default:
				pace = 2;
		}
		return pace;
    }
    
    public void onLoadGameClick() {
    	presenter.onLoadGameClick();
    
    }
}
